package it.unical.canonaco.rende.igpe.game.cbv.guilayer.components;

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.imageio.ImageIO;

/**
 * Basic Image cache. It doesn't take into account memory
 * @author Fabio
 *
 */
public class ImageCache {

	HashMap<String, Image> cache = new HashMap<String, Image>();
	
	
	public ImageCache() {
		
	}
	
	public Image getImage(String path){
		
		String.valueOf(path.hashCode());
		
		if(!cache.containsKey(path))
			if(!loadImage(path))
				return null;
		
		return cache.get(path);
		
	}
	
	private boolean loadImage(String path){
		
		try {
			cache.put(path, ImageIO.read(new File(path)));
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	
	public void putImage(String path, Image image){
		cache.put(path, image);
	}
	
	public void clearCache(){
		cache = new HashMap<String, Image>();
	}
	
}
